#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT  10
#ifdef _WIN32
#include <windows.h>
#endif
typedef char SElemType;
typedef struct
{
	SElemType* base;
	SElemType* top;
	int stacksize;
}SqStack;

int InitStack(SqStack *S)
{
	(*S).base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
	(*S).top = (*S).base;
	(*S).stacksize = STACK_INIT_SIZE;

}

void Push(SqStack *S, SElemType e)
{
	if ((*S).top - (*S).base >= (*S).stacksize) {
		(*S).base = (SElemType*)realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType));
		(*S).top = (*S).base + (*S).stacksize;
		(*S).stacksize += STACKINCREMENT;
	}
	fflush(stdin);
	printf("请输入要入栈的元素的值：");
	*((*S).top)++ = e;
}

int Pop(SqStack *S,SElemType *e)
{
	*e = *--(*S).top;
	return OK;
}

int GetTop(SqStack *S, SElemType *e)
{
	if (S->top>S->base)
	{
		*e = *(S->top - 1);
		return OK;
	}
	else return ERROR;
}

void display()
{}

int main(void)
{
#ifdef _WIN32
	SetConsoleOutputCP(65001);
#endif
	int choice;
	char e;
	SqStack *s;
	do
	{
		printf("==================\n");
		printf("         0:退出\n");
		printf("         1:初始化栈\n");
		printf("         2:入栈\n");
		printf("         3:出栈\n");
		printf("         4:读取栈顶元素\n");
		printf("         5:显示栈中元素\n");
		printf("==================\n");
		printf("输入操作选择代码(0-5):");
		scanf_s("%d", &choice);

		while (choice < 0 || choice>5) {
			printf("输入有误，请重新输入(0-5):");
			scanf_s("%d",&choice);
		}
		switch (choice)
		{
		    case 0:exit(1);
			case 1:InitStack(s); break;
			case 2:printf("2\n"); Push(s, e); break;
			case 3:Pop(s, e); printf("出栈元素的值是：%c\n", e); break;
			case 4:GetTop(s, e); printf("栈顶元素的值是：%c\n", e); break;
		}
	} while (choice);


	return 0;
}